﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExamAnalysis.Model;

namespace ExamAnalysis.Core.Statistics
{
    public static class Cache
    {
        public enum CacheType
        {
            //MacroStudentReport
            MacroStudentReport_RankOfClass = 1,
            MacroStudentReport_PercentOfClass = 2,
            MacroStudentReport_RankOfGrade = 3,
            MacroStudentReport_PercentOfGrade = 4,
            MacroStudentReport_RankOfClassType = 5,
            MacroStudentReport_PercentOfClassType = 6,
            MacroStudentReport_StandardizedScore = 7,
        };

        public static bool HasCache(CacheType cacheType, string objectType, int objectID, string subjectType, int? subjectID)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                return db.ReportCache.Where(p =>
                    p.CacheType == (int)cacheType &&
                    p.ObjectType == objectType &&
                    p.ObjectID == objectID &&
                    p.SubjectType == subjectType &&
                    p.SubjectID == subjectID)
                    .Count() > 0;
            }
        }

        public static void WriteCache(string cacheValue, CacheType cacheType, string objectType, int objectID, string subjectType, int? subjectID)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                db.ReportCache.InsertOnSubmit(new ReportCache()
                {
                    CacheType = (int)cacheType,
                    ObjectType = objectType,
                    ObjectID = objectID,
                    SubjectType = subjectType,
                    SubjectID = subjectID,
                    CacheValue = cacheValue,
                    TimeStamp = DateTime.Now
                });
                db.SubmitChanges();
            }
        }

        public static string ReadCache(CacheType cacheType, string objectType, int objectID, string subjectType, int? subjectID)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                return db.ReportCache.Where(p =>
                    p.CacheType == (int)cacheType &&
                    p.ObjectType == objectType &&
                    p.ObjectID == objectID &&
                    p.SubjectType == subjectType &&
                    p.SubjectID == subjectID)
                    .OrderByDescending(p => p.TimeStamp)
                    .First().CacheValue;
            }
        }

        public static void ClearCache(CacheType cacheType, string objectType, int objectID, string subjectType, int? subjectID)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var results = db.ReportCache.Where(p =>
                    p.CacheType == (int)cacheType &&
                    p.ObjectType == objectType &&
                    p.ObjectID == objectID &&
                    p.SubjectType == subjectType &&
                    p.SubjectID == subjectID);
                db.ReportCache.DeleteAllOnSubmit(results);
                db.SubmitChanges();
            }
        }

        public static void ClearCache()
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                db.ReportCache.DeleteAllOnSubmit(db.ReportCache.ToList());
                db.SubmitChanges();
            }
        }
    }
}
